using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using SimpleInjector;

namespace Beaker.Injection.Desktop.SimpleInjector
{
	internal sealed class QueryContainer
		: IQueryContainer
	{
		private readonly Container _container;

		internal QueryContainer(Container container)
		{
			Contract.Requires<ArgumentNullException>(container != null);
			_container = container;
		}

		public TService GetInstance<TService>() where TService : class
		{
			try
			{
				return _container.GetInstance<TService>();
			}
			catch (ActivationException activationException)
			{
				throw new Microsoft.Practices.ServiceLocation.ActivationException(activationException.Message, activationException);
			}
		}

		public object GetInstance(Type serviceType)
		{
			try
			{
				return _container.GetInstance(serviceType);
			}
			catch (ActivationException activationException)
			{
				throw new Microsoft.Practices.ServiceLocation.ActivationException(activationException.Message, activationException);
			}
		}

		public IEnumerable<TService> GetAllInstances<TService>()
		{
			try
			{
				return _container.GetAllInstances<TService>();
			}
			catch (ActivationException activationException)
			{
				throw new Microsoft.Practices.ServiceLocation.ActivationException(activationException.Message, activationException);
			}
		}

		public IEnumerable<object> GetAllInstances(Type serviceType)
		{
			try
			{
				return _container.GetAllInstances(serviceType);
			}
			catch (ActivationException activationException)
			{
				throw new Microsoft.Practices.ServiceLocation.ActivationException(activationException.Message, activationException);
			}
		}
	}
}